﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JGLib.States;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace TrafficTrouble.States
{
    public class MainMenuState : State
    {
        private enum Content { ButtonTex, ButtonHighlightTex, ButtonFont, Background }

        private const float ButtonMargin = 50;

        private int buttonPressed = -1;
        private int button = -1;

        public MainMenuState()
            : base(typeof(MainMenuState)) 
        {
            
        }

        protected override void LoadContent()
        {
            this.LoadTexture("MainMenu/MenuButton", Content.ButtonTex);
            this.LoadTexture("MainMenu/MenuButtonHighlight", Content.ButtonHighlightTex);
            this.LoadContent<SpriteFont>("MainMenu/MenuFont", Content.ButtonFont);
            this.LoadTexture("LevelSelection/Background", Content.Background);
        }

        protected override void Update(double gameTime)
        {
            float buttonWidth = this.GetTexture(Content.ButtonTex).Width;
            float buttonHeight = this.GetTexture(Content.ButtonTex).Height;
            float totalHeight = buttonHeight * 3 + ButtonMargin * 2;

            float x = (Game.Width - buttonWidth) / 2;
            float topY = (Game.Height - totalHeight) / 2;

            foreach (TouchLocation touch in Touch.Touches)
            {
                if (touch.Position.X >= x && touch.Position.X < x + buttonWidth)
                {
                    if (touch.Position.Y >= topY && touch.Position.Y < topY + buttonHeight)
                    {
                        if (touch.State == TouchLocationState.Pressed || touch.State == TouchLocationState.Moved)
                            buttonPressed = 0;
                        else if (touch.State == TouchLocationState.Released)
                            button = 0;
                    }
                    else if (touch.Position.Y >= topY + buttonHeight + ButtonMargin
                        && touch.Position.Y < topY + buttonHeight * 2 + ButtonMargin)
                    {
                        if (touch.State == TouchLocationState.Pressed || touch.State == TouchLocationState.Moved)
                            buttonPressed = 1;
                        else if (touch.State == TouchLocationState.Released)
                            button = 1;
                    }
                    else if (touch.Position.Y >= topY + (buttonHeight + ButtonMargin) * 2
                        && touch.Position.Y < topY + (buttonHeight + ButtonMargin) * 2 + buttonHeight)
                    {
                        if (touch.State == TouchLocationState.Pressed || touch.State == TouchLocationState.Moved)
                            buttonPressed = 2;
                        else if (touch.State == TouchLocationState.Released)
                            button = 2;
                    }
                    else
                    {
                        if (touch.State == TouchLocationState.Pressed || touch.State == TouchLocationState.Moved)
                            buttonPressed = -1;
                        else if (touch.State == TouchLocationState.Released)
                            button = -1;
                    }
                }
                else
                {
                    if (touch.State == TouchLocationState.Pressed || touch.State == TouchLocationState.Moved)
                        buttonPressed = -1;
                    else if (touch.State == TouchLocationState.Released)
                        button = -1;
                }
                if (button == 0 && buttonPressed == 0)
                {
                    this.SwitchToState(typeof(LevelSelectionState));
                    this.buttonPressed = this.button = -1;
                }
                else if (button == 2 && buttonPressed == 2)
                {
                    Game.ExitGame();
                    this.buttonPressed = this.button = -1;
                }
            }

            base.Update(gameTime);
        }

        protected override void Draw(SpriteBatch g)
        {
            g.Draw(this.GetTexture(Content.Background), new Rectangle(0, 0, Game.Width, Game.Height), Color.White);

            float buttonWidth = this.GetTexture(Content.ButtonTex).Width;
            float buttonHeight = this.GetTexture(Content.ButtonTex).Height;
            float totalHeight = buttonHeight * 3 + ButtonMargin * 2;

            float x = (Game.Width - buttonWidth) / 2;
            float topY = (Game.Height - totalHeight) / 2;

            g.Draw(this.GetTexture(Content.ButtonTex), new Vector2(x, topY), Color.White);
            g.Draw(this.GetTexture(Content.ButtonTex), new Vector2(x, topY + buttonHeight + ButtonMargin), Color.White);
            g.Draw(this.GetTexture(Content.ButtonTex), new Vector2(x, topY + (buttonHeight + ButtonMargin) * 2), Color.White);

            if (buttonPressed != -1)
                g.Draw(this.GetTexture(Content.ButtonHighlightTex),
                    new Vector2(x, topY + (buttonHeight + ButtonMargin) * buttonPressed), Color.White);

            SpriteFont font = this.GetContent<SpriteFont>(Content.ButtonFont);
            Vector2 playSize = font.MeasureString("Play"),
                quitSize = font.MeasureString("Quit");

            g.DrawString(font, "Play", new Vector2((Game.Width - playSize.X) / 2, topY + buttonHeight / 2 - playSize.Y / 2), Color.White);
            g.DrawString(font, "Quit", new Vector2((Game.Width - playSize.X) / 2, topY + (buttonHeight + ButtonMargin) * 2 + buttonHeight / 2 - playSize.Y / 2), Color.White);
        }
    }
}
